package com.gxuwz.controller;

import com.gxuwz.entity.Department;
import com.gxuwz.entity.vo.PageVo;
import com.gxuwz.result.Code;
import com.gxuwz.result.PageInfo;
import com.gxuwz.result.ResultInfo;
import com.gxuwz.service.ClassesService;
import com.gxuwz.service.DepartmentService;
import com.gxuwz.service.InstructorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @Description:二级学院信息管理模块
 */

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private InstructorService instructorService;

    @Autowired
    private ClassesService classesService;

    /**
     * @Description:查询二级学院信息，并跳转至二级学院信息管理页面
     * @Param:  [com.gxuwz.result.PageInfo, org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Department]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/list")
    public ModelAndView departmentList(PageInfo pageInfo, ModelAndView modelAndView,Department department){
        System.out.println(pageInfo);
        System.out.println(department);
        PageVo pageVo = departmentService.getDepartmentList(department, pageInfo);
        modelAndView.setViewName("departmentManage/departmentManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:跳转至添加二级学院信息页面
     * @Param:  [org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/addDepartmentPage")
    public ModelAndView queryIds(ModelAndView modelAndView){
        List<String> ids = departmentService.selectDepartmentIds();
        System.out.println(ids);
        modelAndView.addObject("depIds",ids);
        modelAndView.setViewName("departmentManage/addDepartment");
        return modelAndView;
    }

    /**
     * @Description:添加二级学院信息，并跳转至二级学院信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Department]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/addDepartment")
    public ModelAndView addDepartment(ModelAndView modelAndView,Department department){
        boolean flag = departmentService.addDepartmentInfo(department);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_OK,"新增成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.ADD_ERR,"新增失败"));
        }
        PageVo pageVo = departmentService.getDepartmentList(null, pageInfo);
        modelAndView.setViewName("departmentManage/departmentManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:跳转至添加二级学院信息管理页面
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @GetMapping("/updateDepartmentPage")
    public ModelAndView queryDepartmentOne(String depId,ModelAndView modelAndView){
        System.out.println(depId);
        Department departmentOne = departmentService.getDepartmentOne(depId);
        System.out.println(departmentOne);
        List<String> ids = departmentService.selectDepartmentIds();
        modelAndView.addObject("depIds",ids);
        modelAndView.addObject("departmentOne",departmentOne);
        modelAndView.setViewName("departmentManage/editDepartment");
        return modelAndView;
    }

    /**
     * @Description:实现添加二级学院信息，并跳转至二级学院信息管理页面
     * @Param:  [org.springframework.web.servlet.ModelAndView, com.gxuwz.entity.Department, java.lang.String]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/updateDepartment")
    public ModelAndView updateDepartment(ModelAndView modelAndView,Department department,String oldId){
        System.out.println(oldId);
        boolean flag = departmentService.updateDepartmentInfo(department,oldId);
        PageInfo pageInfo = new PageInfo();
        if(flag){
            modelAndView.addObject("resultInfo",new ResultInfo(Code.UPDATE_OK,"修改成功"));
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.UPDATE_OK,"修改失败"));
        }
        PageVo pageVo = departmentService.getDepartmentList(null, pageInfo);
        modelAndView.setViewName("departmentManage/departmentManage");
        modelAndView.addObject("pageVo",pageVo);
        return modelAndView;
    }

    /**
     * @Description:实现删除单一二级学院信息，并跳转至二级学院信息管理页面
     * @Param:  [java.lang.String, org.springframework.web.servlet.ModelAndView]
     * @Return: org.springframework.web.servlet.ModelAndView
     */

    @RequestMapping("/deleteDepartmentOne")
    public ModelAndView deleteDepartmentOne(String depId, ModelAndView modelAndView){
        boolean flagInstructor = instructorService.selectInstructorExist(depId);
        boolean flagClasses = classesService.selectClassesExist(depId);
        PageInfo pageInfo = new PageInfo();
        if(flagClasses&&flagInstructor){
            boolean flag = departmentService.deleteDepartmentOne(depId);
            if(flag){
                modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_OK,"删除成功"));
            } else {
                modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"删除失败"));
            }
            PageVo pageVo = departmentService.getDepartmentList(null, pageInfo);
            modelAndView.setViewName("departmentManage/departmentManage");
            modelAndView.addObject("pageVo",pageVo);
            return modelAndView;
        } else {
            modelAndView.addObject("resultInfo",new ResultInfo(Code.DELETE_ERR,"删除失败,请先处理该二级学院下的班级和辅导员"));
            PageVo pageVo = departmentService.getDepartmentList(null, pageInfo);
            modelAndView.setViewName("departmentManage/departmentManage");
            modelAndView.addObject("pageVo",pageVo);
            return modelAndView;
        }
    }

}
